package com.mhkj.revise.draft.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.common.util.ItemHelper;
import com.mhkj.revise.draft.service.DraftService;
import com.mhkj.revise.plan.entity.Plan;
import com.mhkj.revise.plan.repository.PlanRepository;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

/**
 * 草稿箱
 * @author mics
 * @date 2018年8月28日
 * @version 1.0
 */
@Service
public class DraftServiceImpl extends BaseServiceImpl<Plan> implements DraftService{
	@Resource
	private PlanRepository planRepository;
	@Resource
	private ItemHelper itemHelper;
	
	@Resource
	public void setPlanRepository(PlanRepository planRepository) {
		super.setBaseRepository(planRepository);
	}


	@Override
	public Pagination<Plan> listByPage(Plan plan, int pageIndex, int pageSize) {
		Specification<Plan> spec = new Specification<Plan> () {  
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<Plan> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
	          List<Predicate> lstPredicates = new ArrayList<Predicate>();
			  if(itemHelper.getCurrentUserItems().size() > 0 ){
					Predicate p3=criteriaBuilder.and(root.get("item").in(itemHelper.getCurrentUserItems()));
 					Predicate p4=criteriaBuilder.and(root.get("item").isNull());
 		            lstPredicates.add(criteriaBuilder.or(p3,p4));
			  }else{
 					Predicate p4=criteriaBuilder.and(root.get("item").isNull());
		            lstPredicates.add(p4);
			  }
              if(!StringUtils.isEmpty(plan.getName())){
                  Predicate p1=criteriaBuilder.like(root.get("name").as(String.class), "%"+plan.getName()+"%");
                  lstPredicates.add(p1);
              }
              if(plan.getPlanType() != null){
                  Join<Object, Object> planType = root.join("planType",JoinType.INNER);
                  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), plan.getPlanType().getId());
                  lstPredicates.add(p1);
              }
              Predicate p1=criteriaBuilder.equal(root.get("state").as(Integer.class),0);
              lstPredicates.add(p1);
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<Plan> pageT =  planRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<Plan> pagination = new Pagination<Plan>();
		pagination.setTotal(planRepository.count(spec));
		List<Plan> Plans = pageT.getContent();
		pagination.setData(Plans);
		return pagination;
	}

}
